/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import java.util.Iterator;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Collaboration;
import org.eclipse.uml2.uml.CollaborationUse;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;
import com.cea.papyrus.umlutils.Package;
import com.cea.papyrus.umlutils.Type;

/**
 * 
 */
public class CollaborationUseTypeSection extends PropertyViewSection {


	/**
	 * 
	 */
	private CCombo comboType;
	
	/**
	 * 
	 */
	private SelectionListener comboListener;

	/**
	 * 
	 * 
	 * @param tabbedPropertySheetPage 
	 * @param parent 
	 */
	@Override
	public void createControls(Composite parent,
			TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		Composite composite = getWidgetFactory()
		.createFlatFormComposite(parent);
		FormData data;

		getWidgetFactory().createCLabel(composite, "Type:"); //$NON-NLS-1$

		comboType = getWidgetFactory().createCCombo(composite, SWT.NONE);
		data = new FormData();
		data.left = new FormAttachment(0, STANDARD_LABEL_WIDTH);
		data.top = new FormAttachment(0, 0);
		comboType.setLayoutData(data);

		comboType.addSelectionListener(comboListener = new SelectionListener () {
			public void widgetSelected(SelectionEvent e) {
				org.eclipse.uml2.uml.Collaboration newType = (org.eclipse.uml2.uml.Collaboration)(comboType.getData(comboType.getItem(comboType.getSelectionIndex())));
				((CollaborationUse) getElement()).setType(newType);
			}

			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
	}



	/*
	 * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
	 */
	/**
	 * 
	 */
	@Override
	public void refresh() {
		if(!comboType.isDisposed()){
		
			if((getElement() instanceof CollaborationUse)) {
				comboType.removeAll();
				Package context = new Package(((CollaborationUse)getElement()).getNearestPackage());
				Set<Type> types=null;
				types= context.getAccessibleTypes();
				org.eclipse.uml2.uml.Collaboration currentType = null;
				currentType=((CollaborationUse)getElement()).getType();
	
				String currentTypeName = null;
				if(currentType != null) {
					currentTypeName = currentType.getQualifiedName();
				}

				Iterator<Type> typesIt = types.iterator();
				int comboIndex = 0;
				//add <undefined>
				comboType.add("<Undefined>");
				comboType.setData(null);
				comboIndex = comboIndex + 1;
				while(typesIt.hasNext()) {

					Type type = typesIt.next();
					if (type.getUml2Element() instanceof Collaboration){
					comboType.add(type.getQualifiedName());
					comboType.setData(type.getQualifiedName(), type.getUml2Type());

					// Select the current property type in the list
					if((currentTypeName != null) && currentTypeName.equals(type.getQualifiedName())) {
						comboType.select(comboIndex);
					}

					comboIndex = comboIndex + 1;
					}
				}
			}
		}

	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	/**
	 * 
	 */
	public void dispose(){
		super.dispose();
		if(comboType!=null && !comboType.isDisposed())
			comboType.removeSelectionListener(comboListener);
	}

}

